package com.jboost.modules.app.service.strategry.xm;

import com.google.common.collect.Maps;
import com.google.gson.JsonObject;
import com.jboost.common.util.EncryptSha256Util;
import com.jboost.common.util.HttpRestUtils;
import com.jboost.common.util.JSONUtil;
import com.jboost.modules.app.constant.Strategy;
import com.jboost.modules.app.dto.XmFieldDto;
import com.jboost.modules.system.mapper.SysUserDepartMapper;
import com.jboost.modules.system.util.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.apache.http.client.HttpClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

import static com.jboost.modules.app.service.strategry.xm.XmUserQuery.getParams;

@Service
@Slf4j
public abstract class AbstractXmUser<T, V> implements Strategy<T,V> {
    protected HttpHeaders jdyHeaders;
    {
        jdyHeaders = new HttpHeaders();
        jdyHeaders.set("Authorization","Bearer "+ "smLHulipnrxt6Lx9x1GqA0CyzpSl4k2o");

    }
    @Autowired
    protected SysUserDepartMapper sysUserDepartMapper;
    protected final static String client_id = "3dd8aa859635c8937ba5ff53bf3c9fb5e903d8c3750878b01de17308ee5af5c7986a316e3962cd8c";
    protected final static String client_secret = "9077a72553c1156a6b9a426f387bb1ac02673ccfb7b1cb34ac710b7182be92be3c29d684421e40c8";
    protected final static String field_api = "https://api-sandbox.xiaoman.cn/v1/company/fields";
    protected final static String select_api = "https://api-sandbox.xiaoman.cn/v1/company/fields/selector";
    protected final static String select_sj_api = "https://api-sandbox.xiaoman.cn/v1/opportunity/fields";
    protected final static String app_id = "648413a12b740b0008c630a7";
    protected final static String form_id = "641d6de6031e8a0009801efc";
    protected final static String tokenUrl = "https://api-sandbox.xiaoman.cn/v1/oauth2/access_token";
    protected final static String customersUrl = "https://api-sandbox.xiaoman.cn/v1/company/list";
    protected final static String sjUrl = "https://api-sandbox.xiaoman.cn/v1/opportunity/list";
    protected final static String customerDetailUrl = "https://api-sandbox.xiaoman.cn/v1/company/info";


    protected Map<String, XmFieldDto.DataBean> getFieldsEnumMap(String field,String token) throws IOException {
        HttpHeaders headers = new HttpHeaders();
        HashMap<String, Object> paramMap = Maps.newHashMap();
        paramMap.put("field",field);
        headers.set("Authorization","Bearer "+ token);
        String response = HttpRestUtils.get(field_api, paramMap, headers);
        XmFieldDto xmFieldDto = JSONUtil.fromJson(response, XmFieldDto.class);
        Map<String, XmFieldDto.DataBean> collect = xmFieldDto.getData().stream().collect(Collectors.toMap(XmFieldDto.DataBean::getId, Function.identity(), (a, b) -> a));
        return collect;
    }


    public static Map<String, XmFieldDto.DataBean> getFieldsSelectMap(String field,String api) throws IOException {
        HashMap<String, Object> paramMap = Maps.newHashMap();
        paramMap.put("type",field);
        Request request1 = new Request.Builder()
                .url(api+getParams(paramMap)).addHeader("Authorization","Bearer " + getHeader(field)).get()
                .build();
        OkHttpClient client = new OkHttpClient();
        Response execute = client.newCall(request1).execute();
        System.out.println(execute.body().string());
        return null;

    }


    protected static String getHeader(String scope) throws IOException {
        HashMap<String, Object> paramMap = Maps.newHashMap();
        paramMap.put("grant_type","password");
        paramMap.put("client_id",client_id);
        paramMap.put("client_secret",client_secret);
        paramMap.put("scope",scope);
        paramMap.put("username","admin@huixinalu.com");
        paramMap.put("password", EncryptSha256Util.getSha256Str("Abc123456"));
        HttpHeaders headers = new HttpHeaders();
        headers.set("Authorization","Basic "+ Base64.getEncoder().encodeToString((client_id+":"+client_secret).getBytes()));
        String response = HttpRestUtils.post(tokenUrl, paramMap, headers);
        HashMap hashMap = JSONUtil.fromJson(response, HashMap.class);
        return (String) hashMap.get("access_token");
    };



    public HashMap<String, Object> getParam() {
        HashMap<String, Object> paramMap = Maps.newHashMap();
        paramMap.put("app_id",app_id);
        paramMap.put("entry_id",form_id);
        return paramMap;
    };
}
